package com.irdstudio.efp.batch.service.impl;

import java.net.InetAddress;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.batch.service.facade.NlsProcessBizAlrmService;
import com.irdstudio.efp.esb.service.bo.req.hlw.ReqAlarmJudgementBean;
import com.irdstudio.efp.esb.service.bo.resp.hlw.RespAlarmJudgementBean;
import com.irdstudio.efp.esb.service.facade.hlw.AlarmJudgementService;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.efp.nls.common.constant.QueueTaskEnums;
import com.irdstudio.efp.nls.service.facade.queue.NlsProcessBizService;

/**
 * 网贷业务过程告警服务
 * 
 * @author huangwb
 * @since 2019年1月24日 上午11:55:57
 * @version 1.0
 */
@Service("nlsProcessBizAlrmService")
public class NlsProcessBizAlrmServiceImpl implements NlsProcessBizAlrmService {
	private static Logger logger = LoggerFactory.getLogger(NlsProcessBizAlrmServiceImpl.class);
	@Autowired
	@Qualifier("nlsProcessBizService")
	private NlsProcessBizService nlsProcessBizService;
	
	@Autowired
	@Qualifier("alarmJudgementService")
	private AlarmJudgementService alarmJudgementService;
	
	/**
	 * 网贷业务过程告警服务<br>
	 * 1.统计网贷业务过程中状态为失败的总数 <br>
	 * 2.统计网贷业务过程中队列状态为挂起的总数 <br>
	 * 3.调用告警判断服务
	 * 
	 * @throws Exception
	 * @author huangwb
	 * @since 2019年1月24日 上午11:56:26
	 * @version 1.0
	 */
	@Override
	public void noticeIfpAlrmInf() throws Exception {
		String queueTaskState = QueueTaskEnums.QueueTaskStateEnum.FAILURE.getCode();// 执行失败
		logger.debug("根据队列任务状态：【" + QueueTaskEnums.QueueTaskStateEnum.FAILURE.getDesc() + "】查询网贷业务过程表开始");
		List<String> failureList = nlsProcessBizService.queryProcessBizByQueueTaskState(queueTaskState);
		logger.debug("根据队列任务状态：【" + QueueTaskEnums.QueueTaskStateEnum.FAILURE.getDesc() + "】查询网贷业务过程结束，条数为：【" + failureList.size() + "】");

		queueTaskState = QueueTaskEnums.QueueTaskStateEnum.HANG.getCode();// 挂起
		logger.debug("根据队列任务状态：【" + QueueTaskEnums.QueueTaskStateEnum.HANG.getDesc() + "】查询网贷业务过程表开始");
		List<String> hangList = nlsProcessBizService.queryProcessBizByQueueTaskState(queueTaskState);
		logger.debug("根据队列任务状态：【" + QueueTaskEnums.QueueTaskStateEnum.HANG.getDesc() + "】查询网贷业务过程结束，条数为：【" + hangList.size() + "】");
		String alrmInf = "队列任务执行失败条数为【" + failureList.size() + "】，挂起条数为【" + hangList.size() + "】";
		// 调用颖风写的告警判断服务
		ReqAlarmJudgementBean reqAlarmJudgementBean = new ReqAlarmJudgementBean();
		reqAlarmJudgementBean.setMonObjNm("网贷业务");
		reqAlarmJudgementBean.setMonObjSpfTpVal("网贷");
		try {
			reqAlarmJudgementBean.setMonObjLctr(InetAddress.getLocalHost().getHostAddress());// 应用地址
		} catch (Exception e) {
			reqAlarmJudgementBean.setMonObjLctr("efp_batch");
		}
		reqAlarmJudgementBean.setAlrmInf(alrmInf);
		reqAlarmJudgementBean.setAlrmTmstmp(DateUtility.getCurrAppDateTimeString());
		reqAlarmJudgementBean.setMonAlrmLvl("2");
		RespAlarmJudgementBean respAlarmJudgementBean = alarmJudgementService.alarmJudgement(reqAlarmJudgementBean);
		logger.debug("调用告警判断返回码：【" + respAlarmJudgementBean.getRetCd() + "】，返回信息：【"+respAlarmJudgementBean.getRetMsg()+"】");
	}
}
